package com.zyark.web.response;

import java.util.List;

/**
 * ClassName: ResponseUtils  <br/>
 * Function:  <br/>
 * Reason:  <br/>
 * Date: 2017-05-24 13:52 <br/>
 *
 * @author Ku_ker
 * @version 1.0
 * @JDK 1.7
 */
public class ResponseUtils {

    public static Json setToJson(ResponseCode responseCode) {
        Json json = new Json();
        return setToJson(responseCode, json);
    }

    public static Json setToJson(ResponseCode responseCode, Json json) {

        json.setCode(responseCode.getCode());
        json.setMsg(responseCode.getMsg());
        return json;
    }

    public static Json setSuccess(Object obj) {
        Json json = new Json();
        json.setCode(ResponseCode.SUCCESS.getCode());
        json.setMsg(ResponseCode.SUCCESS.getMsg());
        json.setSuccess(true);
        json.setObj(obj);
        return json;
    }

    public static Json setDataAndResponse(List list) {

        if (list.size() > 0) {
            return ResponseUtils.setSuccess(list);
        } else {
            return ResponseUtils.setToJson(ResponseCode.NO_DATA);
        }
    }

    /**
     * @param responseCode
     * @param msg
     * @param isReplace
     *         msg是否替代Json的msg
     *
     * @return
     */
    public static Json setJsonAndMsg(ResponseCode responseCode, String msg, boolean isReplace) {

        Json json = new Json();
        json.setCode(responseCode.getCode());
        if (isReplace) {
            json.setMsg(msg);
        } else {
            json.setMsg(msg + responseCode.getMsg());
        }

        return json;
    }

}
